package phone.qyg.com.qyg.activity;

import android.app.AlertDialog;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.app.base.tool.BaseActivity;
import com.app.config.AppHttpKey;
import com.app.config.ApplicationConfig;
import com.app.utils.PromptManager;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;

import phone.qyg.com.qyg.R;
import phone.qyg.com.qyg.model.Base;
import phone.qyg.com.qyg.utils.AppHttpRequest;
import phone.qyg.com.qyg.utils.PopuTypeSelectState;


/**
 * 作者：lblbh on 2017/5/25 17:03
 * 邮箱：lanbuhan@163.com
 * 功能：意见反馈
 */
@ContentView(R.layout.opinion)
public class OpinionActivity extends BaseActivity implements TextWatcher {

    @ViewInject(R.id.back)
    TextView back;

    @ViewInject(R.id.submit)
    TextView submit;

    @ViewInject(R.id.feedback_type)
    TextView feedback_type;

    @ViewInject(R.id.title)
    EditText title;

    @ViewInject(R.id.content)
    EditText content;

    @ViewInject(R.id.phone)
    EditText phone;

    Base typeBase;

    @Event(value = {R.id.back,R.id.feedback_type,R.id.submit})
    private void viewOnClick(View v){
        switch (v.getId()){
            case R.id.back:
                this.finish();
                break;
            case R.id.feedback_type:
                PopuTypeSelectState.showDialog(this,ApplicationConfig.PRO_BELM_TYPE, new PopuTypeSelectState.OnAlertOnClick() {
                    @Override
                    public void onClick(Base theme, int dataType, AlertDialog showAlert) {
                        if(theme.type == 1){
                            typeBase = theme;
                            feedback_type.setText(theme.title);
                        }
                        onTextChanged(null,0,0,0);
                        dismissDialog(showAlert);
                    }
                });
                break;
            case R.id.submit:
                String titleStr = getEditToString(title);
                String contentStr = getEditToString(content);
                String phoneStr = getEditToString(phone);

                if(null == typeBase){
                    PromptManager.showToast(R.string.feedback_hint_key);
                    return;
                }
                mapKeys.put(AppHttpKey.TYPE,String.valueOf(typeBase.id));
                mapKeys.put(AppHttpKey.TITLE,titleStr);
                mapKeys.put(AppHttpKey.CONTENT,contentStr);
                mapKeys.put(AppHttpKey.CONTACT,phoneStr);

                AppHttpRequest.appQuitState(ApplicationConfig.FEED_BACK,
                        mapKeys,
                        new AppHttpRequest.OnAppHttpListener() {
                            @Override
                            public void onAppHttpState(boolean isState, String result) {
                                if(isState){
                                    PromptManager.showToast(R.string.thank_opinion);
                                    OpinionActivity.this.finish();
                                }
                            }
                        });
                break;
        }
    }

    @Override
    public void initData(Bundle savedInstanceState) {}
    @Override
    public void initSucessView() {}
    @Override
    public void initViewHandler() {
        title.addTextChangedListener(this);
        content.addTextChangedListener(this);
        phone.addTextChangedListener(this);
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        submit.setEnabled(false);
        submit.setBackgroundResource(R.drawable.ash_btn_frame);
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {

        if(isEmptyOrHint(title,R.string.opinion_title_hint) &&
                isEmptyOrHintMinCount(content,R.string.opinion_content_hint,1) &&
                isEmptyOrHint(phone,R.string.phone_hint_opinion ) &&
                (null != typeBase)){
            submit.setEnabled(true);
            submit.setBackgroundResource(R.drawable.blue_btn_frame);
        }else{
            submit.setEnabled(false);
            submit.setBackgroundResource(R.drawable.ash_btn_frame);
        }
    }

    @Override
    public void afterTextChanged(Editable s) {}
}
